package com.ifcar.app.activity.comment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.ifcar.app.fragment.NRTabFragment;
import com.ifcar.app.fragment.NRTabFragment.TabManager.TabInfo;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.view.LoadingListView;
import com.ifcar.app.view.MyTabWidget;
import com.ifcar.app.view.MyTabWidget.AnimationViewCallback;
import com.ifcar.app.activity.R;

public class CommentRankTabFragment extends NRTabFragment {
    public static final String TAG = "comment_rank";
    
	public static final String TAB_COMMENT_TOP = "comment_top";

	public static final String TAB_COMMENT_DAY = "comment_day";

	public static final String TAB_COMMENT_WEEK = "comment_week";

	private LoadingListView mLoadingListView;

	private String mLastTab;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.comment_layout, container, false);
	}

	@Override
	public int getContainerId() {
		return R.id.realtabcontent;
	}

	@Override
	public void onViewCreatedNR(View view, Bundle savedInstanceState) {
		super.onViewCreatedNR(view, savedInstanceState);
		mLoadingListView = (LoadingListView) view
				.findViewById(R.id.rank_comment_loading_list);

		LayoutInflater inflate = LayoutInflater.from(getActivity());
		View moreView = inflate.inflate(R.layout.more_layout,
				mLoadingListView.getListView(), false);
		mLoadingListView.setLoadingMoreView(moreView, R.id.show_more_button);
	}

	@Override
	public void onTabSelected(boolean first) {
		
		super.onTabSelected(first);
	}

	@Override
	public void onTabChanged(String tabId) {
		super.onTabChanged(tabId);

		if (!TextUtils.isEmpty(mLastTab)) {
            CommentRankFragment f = (CommentRankFragment) getTabManager().getTabInfos()
                    .get(mLastTab).getFragment();
			f.detachLoadingListView();
		}

		mLastTab = tabId;

        CommentRankFragment f = (CommentRankFragment) getTabManager().getTabInfos()
                .get(tabId).getFragment();

		f.attachLoadingListView(mLoadingListView);
	}

	@Override
	protected void initTabhost(TabManager tabManager, TabHost tabHost) {
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		MyTabWidget tabWidget = (MyTabWidget) tabHost.getTabWidget();
		View animationView = inflater.inflate(
				R.layout.comment_tab_indicator_bg, null);

		tabWidget.setAnimaitonView(animationView, new AnimationViewCallback() {

			@Override
			public void onPrepareMeasure(View currentTabView, View animationView) {
				// View indicatorView =
				// currentTabView.findViewById(R.id.comment_tab_indicator);
				View indicatorBgView = animationView
						.findViewById(R.id.indicator_bg);
				ViewGroup.LayoutParams lp = indicatorBgView.getLayoutParams();
				lp.width = indicatorBgView.getBackground().getIntrinsicWidth();// indicatorView.getMeasuredWidth();
				lp.height = indicatorBgView.getBackground()
						.getIntrinsicHeight();// indicatorView.getMeasuredHeight();
			}

			@Override
			public Animation createAnimation(int current, int last) {
				TranslateAnimation animation = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, last - current,
						Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 0);
				animation.setDuration(60);
				animation.setInterpolator(getActivity(),
						android.R.anim.decelerate_interpolator);
				return animation;
			}

		});

		Bundle args = new Bundle();
		args.putString("type", "top");
		addSpec(tabManager, tabHost, TAB_COMMENT_TOP, R.string.top_comments,
				CommentRankFragment.class, inflater, args);

		args = new Bundle();
		args.putString("type", "day");
		addSpec(tabManager, tabHost, TAB_COMMENT_DAY,
				R.string.everyday_comments_rank, CommentRankFragment.class,
				inflater, args);

		args = new Bundle();
		args.putString("type", "week");
		addSpec(tabManager, tabHost, TAB_COMMENT_WEEK,
				R.string.everyweek_comments_rank, CommentRankFragment.class,
				inflater, args);

	}

	private void addSpec(TabManager tabManager, TabHost tabHost, String tag,
			int title, Class clazz, LayoutInflater inflater, Bundle bundle) {
		TabSpec spec = tabHost.newTabSpec(tag);
		View view = inflater.inflate(R.layout.comment_tab_indicator, null);

		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.weight = 1;
		view.setLayoutParams(lp);

		TextView tabIndicator = (TextView) view
				.findViewById(R.id.comment_tab_indicator);
		tabIndicator.setText(title);
		spec.setIndicator(view);

		TabInfo info = new TabInfo(tag, clazz, bundle, false);

		tabManager.addTab(spec, info);
	}

	@Override
	public void onApplyTheme() {
		super.onApplyTheme();

		ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
		Activity context = getActivity();

		TabHost tabHost = getTabHost();
		MyTabWidget tabWidget = (MyTabWidget) tabHost.getTabWidget();

		themeSettingsHelper.setViewBackgroud(context, tabWidget,
				R.drawable.top_column_bg);

		View indicatorBgView = tabWidget.getAnimationView().findViewById(
				R.id.indicator_bg);
		themeSettingsHelper.setViewBackgroud(context, indicatorBgView,
				R.drawable.comment_column_highlight);
		final int count = tabWidget.getTabCount();
		for (int i = 0; i < count; i++) {
			TextView t = (TextView) tabWidget.getChildTabViewAt(i)
					.findViewById(R.id.comment_tab_indicator);
			themeSettingsHelper.setTextViewColor(context, t,
					R.color.column_item_color);
		}

		themeSettingsHelper.setListViewDivider(context,
				mLoadingListView.getListView(), R.drawable.list_divider_line);

        Button moreButton = (Button) mLoadingListView.getLoadingMoreClickView();
        themeSettingsHelper.setViewBackgroud(context, moreButton,
                R.drawable.netease_big_button);
        themeSettingsHelper.setTextViewColor(context, moreButton,
                R.color.list_title_color);
		
	}
}
